shopxx = {
	base : "/shop",
	currencySign : "￥",
	currencyUnit : "元",
	priceScale : "2",
	priceRoundType : "roundHalfUp"
};

// 编辑器
if (typeof(KE) != "undefined") {
	KE.show({
				id : "editor",
				height : "400px",
				items : ['source', '|', 'fullscreen', 'undo', 'redo', 'print',
						'cut', 'copy', 'paste', 'plainpaste', 'wordpaste', '|',
						'justifyleft', 'justifycenter', 'justifyright',
						'justifyfull', 'insertorderedlist',
						'insertunorderedlist', 'indent', 'outdent',
						'subscript', 'superscript', '|', 'selectall', '-',
						'title', 'fontname', 'fontsize', '|', 'textcolor',
						'bgcolor', 'bold', 'italic', 'underline',
						'strikethrough', 'removeformat', '|', 'image', 'flash',
						'media', 'advtable', 'hr', 'emoticons', 'link',
						'unlink'],
				imageUploadJson : shopxx.base + "/admin/file!ajaxUpload.action",
				fileManagerJson : shopxx.base
						+ "/admin/file!ajaxBrowser.action",
				allowFileManager : true,
				autoSetDataMode : true
			});
}

// 货币格式化
function currencyFormat(price) {
	price = setScale(price, shopxx.priceScale, shopxx.priceRoundType);
	return shopxx.currencySign + price + shopxx.currencyUnit;
}

function relink(moduleName, url) {
	$.ajax({
				url : url,
				data : {
					id : moduleName
				},
				dataType : "json",
				type : "POST",
				cache : false,
				success : function(data) {
					$.message({
								type : data.status,
								content : data.message
							});
				}
			});
}

function testConnection(id) {
	$.ajax({
				url : "testConnection.do?id=" + id,
				type : "GET",
				dataType : "json",
				cache : false,
				success : function(data) {
					$.message({
								type : data.status,
								content : data.message
							});
				}
			});
}

$().ready(function() {

	/* ---------- List ---------- */

	var $listForm = $("#listForm");// 列表表单
	if ($listForm.size() > 0) {
		var $searchButton = $("#searchButton");// 查找按钮
		var $allCheck = $("#listTable input.allCheck");// 全选复选框
		var $listTableTr = $("#listTable tr:gt(0)");
		var $idsCheck = $("#listTable input:enabled[name='ids']");// ID复选框
		var $deleteButton = $("#deleteButton");// 删除按钮
		var $saveButton = $("#saveButton");// 保存按钮
		var $pageNumber = $("#pageNumber");// 当前页码
		var $pageSize = $("#pageSize");// 每页显示数
		var $sort = $("#listTable .sort");// 排序
		var $orderBy = $("#orderBy");// 排序字段
		var $order = $("#order");// 排序方式

		// 全选
		$allCheck.click(function() {
					var $this = $(this);
					if ($this.attr("checked")) {
						if ($idsCheck.length != 0) {
							$idsCheck.attr("checked", true);
							$deleteButton.attr("disabled", false);
							$listTableTr.addClass("checked");
						}
					} else {
						if ($idsCheck.length != 0) {
							$idsCheck.attr("checked", false);
							$deleteButton.attr("disabled", true);
							$listTableTr.removeClass("checked");
						}
					}
				});

		$saveButton.click(function() {
					var trs = $("#listTable tr");
					var data = "ids=";
					for (var i = 2; i <= trs.length; i++) {
						var tr = $("#listTable tr:nth-child(" + i + ")");
						var id = tr.find("#id");
						var priority = tr.find("#priority");
						var is_def = tr.find("#is_def");
						var is_disabled = tr.find("#is_disabled");
						data += "model_id->" + id.val() + "$priority->"
								+ priority.val() + "$is_def->"
								+ is_def.attr("checked") + "$is_disabled->"
								+ is_disabled.attr("checked");
						if (i != trs.length)
							data += "&ids=";
					}
					var url = $(this).attr("url");
					$.ajax({
								url : url,
								data : data,
								type : "POST",
								dataType : "json",
								cache : false,
								success : function(data) {
									if (data.status == "success") {
										$.message({
													type : data.status,
													content : data.message
												});
									}
								}
							});
				});

		// 无复选框被选中时,删除按钮不可用
		$idsCheck.click(function() {
					var $this = $(this);
					if ($this.attr("checked")) {
						$this.parent().parent().addClass("checked");
						$deleteButton.attr("disabled", false);
					} else {
						$this.parent().parent().removeClass("checked");
						var $idsChecked = $("#listTable input[name='ids']:checked");
						if ($idsChecked.size() > 0) {
							$deleteButton.attr("disabled", false);
						} else {
							$deleteButton.attr("disabled", true)
						}
					}
				});

		// 批量删除
		$deleteButton.click(function() {
			var url = $(this).attr("url");
			var $idsCheckedCheck = $("#listTable input[name='ids']:checked");
			$.dialog({
						type : "warn",
						content : "确定要删除吗?",
						ok : "确 定",
						cancel : "取 消",
						modal : true,
						okCallback : batchDelete
					});
			function batchDelete() {
				$.ajax({
							url : url,
							data : $idsCheckedCheck.serialize(),
							type : "POST",
							dataType : "json",
							cache : false,
							success : function(data) {
								if (data.status == "success") {
									$idsCheckedCheck.parent().parent().remove();
								}
								$deleteButton.attr("disabled", true);
								$allCheck.attr("checked", false);
								$idsCheckedCheck.attr("checked", false);
								$.message({
											type : data.status,
											content : data.message
										});
							}
						});
			}
		});
		// 查找
		$searchButton.click(function() {
					$pageNumber.val("1");
					$listForm.submit();
				});

		// 每页显示数
		$pageSize.change(function() {
					$pageNumber.val("1");
					$listForm.submit();
				});

		// 排序
		$sort.click(function() {
					var $currentOrderBy = $(this).attr("name");
					if ($orderBy.val() == $currentOrderBy) {
						if ($order.val() == "") {
							$order.val("asc")
						} else if ($order.val() == "desc") {
							$order.val("asc");
						} else if ($order.val() == "asc") {
							$order.val("desc");
						}
					} else {
						$orderBy.val($currentOrderBy);
						$order.val("asc");
					}
					$pageNumber.val("1");
					$listForm.submit();
				});

		// 排序图标效果
		if ($orderBy.val() != "") {
			$sort = $("#listTable .sort[name='" + $orderBy.val() + "']");
			if ($order.val() == "asc") {
				$sort.removeClass("desc").addClass("asc");
			} else {
				$sort.removeClass("asc").addClass("desc");
			}
		}

		// 页码跳转
		$.gotoPage = function(id) {
			$pageNumber.val(id);
			$listForm.submit();
		}
	}

});